import dict from '../common/core/dict';
import formInfo from '@ohos.app.form.formInfo';
import formItem from '../common/models/cards/formItem';
import formProvider from '@ohos.app.form.formProvider';
import relationalStore from '@ohos.data.relationalStore';
import formBindingData from '@ohos.app.form.formBindingData';
import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
import cardManager from '../common/manager/cardManager';

export default class EntryFormAbility extends FormExtensionAbility {
  onAddForm(want) {
    let formId: string = want.parameters[dict.IDENTITY_KEY] as string;
    let formName: string = want.parameters[dict.NAME_KEY] as string;
    let dimensionFlag: number = want.parameters[dict.DIMENSION_KEY] as number;
    cardManager.createRdbStore(this.context).then((rdbStore: relationalStore.RdbStore) => {
      let form: formItem = new formItem();
      form.formId = formId;
      form.formName = formName;
      form.dimension = dimensionFlag;
      cardManager.insertForm(form, rdbStore);
    });
    formProvider.setFormNextRefreshTime(formId, dict.FIVE_MINUTES);
    return formBindingData.createFormBindingData();
  }

  onUpdateForm(formId) {
    cardManager.createRdbStore(this.context).then((rdbStore: relationalStore.RdbStore) => {
      cardManager.updateCardData(rdbStore);
    });
    formProvider.setFormNextRefreshTime(formId, dict.FIVE_MINUTES);
  }

  onRemoveForm(formId: string) {
    cardManager.createRdbStore(this.context).then((rdbStore: relationalStore.RdbStore) => {
      cardManager.deleteFormData(formId, rdbStore);
    });
  }

  onAcquireFormState() {
    return formInfo.FormState.READY;
  }
};